﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using RZD_maps.Modules.MapScale.Views;
using RZD_maps.Modules.MapScale.Models;

using GMap.NET;
using GMap.NET.MapProviders;
using GMap.NET.WindowsPresentation;

using System.Diagnostics;
using RZD_maps.Infrastructure;
using Microsoft.Practices.Prism.Events;

namespace RZD_maps.Modules.MapScale.ViewModels
{
    public class ViewModel
    {
        private View _view;

        public View View
        {
            get
            {
                return _view;
            }
            set
            {
                _view = value;
            }
        }

        public static GMapProvider MapProvider = Model.MapProvider;
        public static PointLatLng Position = new PointLatLng(55.8062181, 38.9791242);//new PointLatLng(54.415238, 40.359397);//new PointLatLng(55.8062181, 38.9791242);

        private IEventAggregator eventAggregator;
        private SubscriptionToken subscriptionToken;

        public ViewModel(IEventAggregator eventAggregator)
        {
            this.eventAggregator = eventAggregator;
            SubscriptCommands();
        }

        public void SubscriptCommands()
        {
            MapPositionItemEvent evnt = eventAggregator.GetEvent<MapPositionItemEvent>();
            if (subscriptionToken != null)
            {
                evnt.Unsubscribe(subscriptionToken);
            }
            subscriptionToken = evnt.Subscribe(MapPositionItemEventHandler, ThreadOption.UIThread, false, MapPositionItemFilter);
        }

        public void MapPositionItemEventHandler(MapPositionItem mapPositionItem)
        {
            Debug.Assert(View != null);
            View.MainMap.Position = new PointLatLng(mapPositionItem.lat, mapPositionItem.lng);
            View.MainMap.Zoom = mapPositionItem.zoom;
        }

        public bool MapPositionItemFilter(MapPositionItem controlData)
        {
            return true;
        }


        
    }
}
